package com.briup.ch12;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author briup-adam
 * @Date 2023/10/25 下午5:37
 * @Description
 **/

public class TcpClientDemo3 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("10.138.124.21", 10086);
            Scanner scanner = new Scanner(System.in);
            String ip = socket.getInetAddress().toString();
            InputStreamReader isr=new InputStreamReader(socket.getInputStream());
            BufferedReader br=new BufferedReader(isr);
            PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
            while (true){
                //发消息
                String msg = scanner.next();
                pw.println(msg);
                System.out.println("我："+msg);
                if ("bye".equals(msg))
                    break;
                //接收消息
                String content = br.readLine();
                System.err.println(ip+":"+content);
                if ("bye".equals(content))
                    break;

            }
            socket.shutdownOutput();
            socket.shutdownInput();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
